from django import template
from page.models import Page

register = template.Library()

def breadcrumps(page):
    return "<ul class='breadcrumb'>"+breadcrumps_generator(find_parent(page))+"<li>"+page.title+"<span class='active'></span></li></ul>"


def breadcrumps_generator(page):
    if page is not None:
        return breadcrumps_generator(find_parent(page))+"<li><a href="+page.get_absolute_url()+">"+page.title+"</a><span class='divider'>/</span></li>"
    else:
        return ""

def find_parent(page):
    try:
        page.parent
        return page.parent
    except Page.DoesNotExist:
        try:
            return find_parent(page.left_sibling)
        except Page.DoesNotExist:
            return None

register.simple_tag(breadcrumps)
